<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Exception;
use think\Request;
use think\Config;

class Knowledge extends AdminBase {
  protected $model, $modelCate;

  public function __construct(Request $request = NULL) {
    parent::__construct($request);
    $this->model = new \app\admin\model\Knowledge();
    $this->modelCate = new \app\admin\model\KnowledgeCate();
  }

  public function _initialize() {
    parent::_initialize();
  }

  public function index() {
    if (!request()->isPost()) {
      $cates = $this->modelCate->order('sort DESC')->column('title', 'id');
      $rows = $this->model
        ->alias('k')
        ->join('__BASE_USER__ u', 'u.id=k.creator_uid', 'left')
        //->join('__KNOWLEDGE_CATE__ c', 'c.id=k.cate_id', 'left')
        ->field('k.*,u.realname')
        ->order('k.sort DESC,k.id DESC')
        ->paginate();

      return view('', ['rows'  => $rows,
                       'cates' => $cates]);
    }
    $sort = input('sort/a', NULL);
    $data = [];
    foreach ($sort as $id => $v) {
      $data[] = ['id'   => $id,
                 'sort' => $v];
    }
    try {
      $this->model->saveAll($data);
    } catch (Exception $e) {
      $this->error($e->getMessage());
    }

    $this->success(lang('success'));

  }

  public function add() {
    return self::edit();
  }

  public function saveCrop() {
      if (request()->isAjax()) {
//          $platform = input('post.platform/s');
          $file = request()->file('image');
          if ($file) {
              $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'crops');
              if ($info) {
                  return [
                      'msg' => 'ok',
                      'src' => str_replace('\\', '/', DS . 'uploads' . DS . 'crops' . DS . $info->getSaveName()),
//                      'platform' => $platform
                  ];
              } else {
                  return [
                      'msg' => $file->getError()
                  ];
              }
          } else {
              return [
                  'msg' => 'file error'
              ];
          }
      } else {
          $this->error('bad request');
      }
  }

  public function edit() {
    $id = input('id/d', 0, 'abs');

    if (!request()->isPost()) {
      $cates = $this->modelCate->order('sort DESC')->column('title', 'id');
      array_unshift($cates,'未分类');

      $row = ($id > 0) ? $this->model->find($id) : [];
      return view('edit', ['row'   => $row,
                           'cates' => $cates]);
    }
    $form = input('form/a', NULL);
//    var_dump($form);
//    exit(0);
    $result = $this->validate($form, 'Knowledge');
    if (TRUE !== $result) {
      $this->error($result);
    }
    $str = strip_tags($form['content']);
    $form['desc'] = mb_substr($str, 0, 100);
    cmf_log('新增/编辑安全知识', $id, $form['title']);
    $this->model->editJump($form, $id, url('index'));

  }

  public function delete() {
    $id = input('id/d', 0, 'abs');
    $r = $this->model->destroy($id);
    if (!$r) $this->error(lang('error') . $this->model->getError());

    cmf_log('删除安全知识', $id);
    $this->success(lang('success'));
  }


}